import { request } from "../request";
import type { PageParams, PageResponse } from "../module/api";

export const blogApi = {
	/**
	 * 获取博客列表
	 * @param data 请求参数
	 */
	async GetBlogs(data: BlogListInput): Promise<PageResponse<BlogListOutput>> {
		var dt = await request.post<PageResponse<BlogListOutput>>(
			"/blog/blogs",
			data,
		);
		return dt.data ?? ({ total: 0 } as PageResponse<BlogListOutput>);
	},
	/**
	 * 获取博客详情
	 * @param id 请求参数
	 */
	async GetBlogById(id: number): Promise<BlogDtoBlogDetailDto> {
		var dt = await request.get<BlogDtoBlogDetailDto>("/blog/blog/" + id);
		return dt.data ?? ({} as BlogDtoBlogDetailDto);
	},
	/**
	 * 添加或修改博客
	 * @param input 添加或修改博客
	 */
	async AddOrUpdateBlog(input: AddOrUpdateBlogRequest): Promise<boolean> {
		var dt = await request.post<boolean>("/blog/addOrUpdateBlog", input);
		return dt.data ?? false;
	},
	/**
	 * 发布博客
	 * @param id 请求参数
	 */
	async Publication(id: number): Promise<boolean> {
		var dt = await request.put<boolean>("/blog/publication/" + id);
		return dt.data ?? false;
	},
	/**
	 * 删除博客
	 * @param id 请求参数
	 */
	async Delete(id: number): Promise<boolean> {
		var dt = await request.delete<boolean>("/blog/" + id);
		return dt.data ?? false;
	},
};

/** 列表接口请求 */
export interface BlogListInput extends PageParams {
	keyword?: null | string;
	summary?: null | string;
	title?: null | string;
	type?: number;
}

/** 列表接口出参 */
export interface BlogListOutput {
	commentCount?: number;
	createdBy?: number;
	createdByName?: null | string;
	id?: number;
	likes?: number;
	publicationTime?: Date | null;
	summary?: null | string;
	title?: null | string;
	type: ArticleType;
	updateBy?: number;
	updateByName?: null | string;
	updatedAt?: Date;
	views?: number;
	status: ArticleStatus;
}

/* 博客详情*/
export interface BlogDtoBlogDetailDto {
	authorName?: null | string;
	avatar?: null | string;
	commentCount?: number;
	content?: null | string;
	createdBy?: number;
	id?: number;
	likes?: number;
	publicationTime?: Date | null;
	summary?: null | string;
	title?: null | string;
	type?: ArticleType;
	updatedAt?: Date;
	views?: number;
	status: ArticleStatus;
}

/** 添加或修改博客 */
export interface AddOrUpdateBlogRequest {
	content: null | string;
	id?: number | null;
	summary: null | string;
	title: null | string;
	type: ArticleType;
}
/** 博客类型 */
export enum ArticleType {
	/**
	 * 原创
	 */
	Original = 1, //原创
	/**
	 * 转载
	 */
	Reprint = 2, //转载
}

export const ArticleTypeLabels: Record<ArticleType, string> = {
	[ArticleType.Original]: "原创",
	[ArticleType.Reprint]: "转载",
};

/** 博客状态 */
export enum ArticleStatus {
	/** 草稿 */
	Draft = 1,
	/** 已发布 */
	Published = 2,
}

export const ArticleStatusLabels: Record<ArticleStatus, string> = {
	[ArticleStatus.Draft]: "草稿",
	[ArticleStatus.Published]: "已发布",
};
